package info.motteke.util;

import static info.motteke.util.StringUtil.*;

import info.motteke.mixivoice4j.MixiVoiceException;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;


public class MixiVoiceProperties extends SecureProperties {

    private static final String WINDOW_WIDTH = "window.width";

    private static final String WINDOW_TABLE_DATE_WIDTH = "window.table.dateWidth";

    private static final String WINDOW_TABLE_VOICE_WIDTH = "window.table.voiceWidth";

    private static final String WINDOW_TABLE_USER_WIDTH = "window.table.userWidth";

    private static final String WINDOW_TABLE_HEIGHT_RATE = "window.table.heightRate";

    private static final String DEFAULT_PROPERTIES = "user.properties";

    private static final String PASSWORD = "password";

    private static final String USER_ID = "userid";

    private static final MixiVoiceProperties instance = new MixiVoiceProperties();

    private static final String WINDOW_HEIGHT = "window.height";

    protected Map<String, Picker> initKeys() {
        Map<String, Picker> m = new HashMap<String, Picker>();
        m.put(USER_ID, ENCRYPT);
        m.put(PASSWORD, ENCRYPT);
        m.put(WINDOW_TABLE_USER_WIDTH, NORMAL);
        m.put(WINDOW_TABLE_VOICE_WIDTH, NORMAL);
        m.put(WINDOW_TABLE_DATE_WIDTH, NORMAL);
        m.put(WINDOW_WIDTH, NORMAL);
        m.put(WINDOW_HEIGHT, NORMAL);
        m.put(WINDOW_TABLE_HEIGHT_RATE, NORMAL);
        return m;
    }

    private MixiVoiceProperties() {
        InputStream is = null;
        try {
            is = getDefault();
            init(is);
        } catch (IOException e){
            return;
        } finally {
            if(is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private InputStream getDefault() throws FileNotFoundException{
        File file = new File(DEFAULT_PROPERTIES);

        if(file.exists()){
            return new FileInputStream(file);
        } else {
            ClassLoader cl = getClass().getClassLoader();
            return cl.getResourceAsStream(DEFAULT_PROPERTIES);
        }
    }

    public void init(InputStream resource) throws IOException {
        if (resource == null) {
            return;
        }
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(resource, "UTF-8"));
            load(br);
        } catch (UnsupportedEncodingException e) {
            throw new MixiVoiceException(e);
        } finally {
            if(br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void store(OutputStream os) throws IOException {
        if(os == null){
            return;
        }
        BufferedWriter bw = null;

        try {
            bw = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
            store(bw);
        } finally {
            if(bw != null){
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static MixiVoiceProperties getInstance() {
        return instance;
    }

    public String getUserId() {
        return get(USER_ID);
    }

    public void setUserId(String userId) {
        set(USER_ID, userId);
    }

    public String getPassWord(){
        return get(PASSWORD);
    }

    public void setPassWord(String password) {
        set(PASSWORD, password);
    }

    public int getWindowTableUserWidth() {
        return parseInt(get(WINDOW_TABLE_USER_WIDTH), 100);
    }

    public void setWindowTableUserWidth(int width) {
        set(WINDOW_TABLE_USER_WIDTH, String.valueOf(width));
    }

    public int getWindowTableDateWidth() {
        return parseInt(get(WINDOW_TABLE_DATE_WIDTH), 100);
    }

    public void setWindowTableDateWidth(int width) {
        set(WINDOW_TABLE_DATE_WIDTH, String.valueOf(width));
    }

    public int getWindowTableVoiceWidth() {
        return parseInt(get(WINDOW_TABLE_VOICE_WIDTH), 100);
    }

    public void setWindowTableVoiceWidth(int width) {
        set(WINDOW_TABLE_VOICE_WIDTH, String.valueOf(width));
    }

    public void store() {
        Writer w = null;
        try {
            w = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(DEFAULT_PROPERTIES), "UTF-8"));
            store(w);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(w != null){
                try {
                    w.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public int getWindowWidth(){
        return parseInt(get(WINDOW_WIDTH), 640);
    }

    public void setWindowWidth(int x) {
        set(WINDOW_WIDTH, String.valueOf(x));
    }

    public int getWindowHeight() {
        return parseInt(get(WINDOW_HEIGHT), 480);
    }

    public void setWindowHeight(int y) {
        set(WINDOW_HEIGHT, String.valueOf(y));
    }

    public int getWindowTableHeightRate() {
        return parseInt(get(WINDOW_TABLE_HEIGHT_RATE), 70);
    }

    public void setWindowTableHeightRate(int rate) {
        set(WINDOW_TABLE_HEIGHT_RATE, String.valueOf(rate));
    }
}
